<?php

namespace App\Extend;
use App\Models\OperationLog;
use Illuminate\Support\Facades\Http;

// https://user.ipweb.cc/api/v2/static-residential.html

/**
 * undocumented class
 *
 * @package default
 * @author
 **/
class IPweb
{
    protected $apiUrl = 'http://api.ipweb.cc:19001';
    protected $token = '29Y1SCZDN3Z91WRSQY9L068XI2T4A1VD';

    /**
     * 创建客户帐号
     *
     * @return array
     * @author
     **/
    public function addUser($userName)
    {
        $params = [
            'userName' => $userName
        ];
        $apiUrl = $this->apiUrl . '/api/agent/addUser';
        $response = Http::withHeaders([
            'Token' => $this->token
        ])->get($apiUrl, $params);

        $result = $response->json();
        OperationLog::create([
            'api' => $apiUrl,
            'params' => json_encode($params, JSON_UNESCAPED_UNICODE),
            'result' => json_encode($result, JSON_UNESCAPED_UNICODE)
        ]);
        return $result;
    }

    /**
     * 修改客户验证密码
     *
     * @return void
     * @author
     **/
    public function updatePwd($customerId, $password)
    {
        $params = [
            'customerId' => $customerId,
            'password' => $password
        ];
        $apiUrl = $this->apiUrl . '/api/agent/updatePwd';
        $response = Http::withHeaders([
            'Token' => $this->token
        ])->get($apiUrl, $params);
    }

    /**
     * 客户关停
     *
     * @return array
     * @author
     **/
    public function changeStatus($customerId, $customerState)
    {
        $params = [
            'customerId' => $customerId,
            'customerState' => $customerState
        ];
        $apiUrl = $this->apiUrl . '/api/agent/changeStatus';
        $response = Http::withHeaders([
            'Token' => $this->token
        ])->get($apiUrl, $params);
        if ($response->status() != 200) {

        }
        $result = $response->json();
        OperationLog::create([
            'api' => $apiUrl,
            'params' => json_encode($params, JSON_UNESCAPED_UNICODE),
            'result' => json_encode($result, JSON_UNESCAPED_UNICODE)
        ]);
        return $result;
    }

    /**
     * 多类型流量查询
     *
     * @return array
     * @author
     **/
    public function getFlowTotalFull($token)
    {
        $apiUrl = $this->apiUrl . '/api/agent/getFlowTotalFull';
        $response = Http::withHeaders([
            'Token' => $token
        ])->get($apiUrl);
        if ($response->status() != 200) {

        }
        return $response->json();
    }

    /**
     * 流量消费详情查询
     *
     * @return array
     * @author
     **/
    public function getFlowConsumeDetail($token, $startDate, $endDate)
    {
        $params = [
            'startDate' => $startDate,
            'endDate' => $endDate
        ];
        $apiUrl = $this->apiUrl . '/api/agent/getFlowConsumeDetail';
        $response = Http::withHeaders([
            'Token' => $token
        ])->get($apiUrl, $params);
        if ($response->status() != 200) {

        }
        return $response->json();
    }

    /**
     * 充值流量
     *
     * @return array
     * @author
     **/
    public function rechargeTrafficCount($customerId, $orderId, $trafficCount)
    {
        $params = [
            'customerId' => $customerId,
            'orderId' => $orderId,
            'trafficCount' => $trafficCount
        ];
        $apiUrl = $this->apiUrl . '/api/agent/rechargeTrafficCount';
        $response = Http::withHeaders([
            'Token' => $this->token
        ])->get($apiUrl, $params);

        $result = $response->json();
        OperationLog::create([
            'api' => $apiUrl,
            'params' => json_encode($params, JSON_UNESCAPED_UNICODE),
            'result' => json_encode($result, JSON_UNESCAPED_UNICODE)
        ]);
        return $result;
    }

    /**
     * 扣除流量
     *
     * @return void
     * @author
     **/
    public function deductionFlow($customerId, $trafficCount)
    {
        $params = [
            'customerId' => $customerId,
            'trafficCount' => $trafficCount
        ];
        $apiUrl = $this->apiUrl . '/api/agent/deductionFlow';
        $response = Http::withHeaders([
            'Token' => ''
        ])->get($apiUrl, $params);
    }

    /**
     * 创建子账号
     *
     * @return array
     * @author
     **/
    public function addChildAccount($username, $pwd, $token)
    {
        $params = [
            'username' => $username,
            'pwd' => $pwd
        ];
        $apiUrl = 'http://api.ipweb.cc:8004/api/agent/childAccount/add';
        $response = Http::withHeaders([
            'Token' => $token
        ])->post($apiUrl, $params);

        $result = $response->json();
        OperationLog::create([
            'api' => $apiUrl,
            'params' => json_encode($params, JSON_UNESCAPED_UNICODE),
            'result' => json_encode($result, JSON_UNESCAPED_UNICODE)
        ]);
        return $result;
    }

    /**
     * 修改子账号密码
     *
     * @return array
     * @author
     **/
    public function updateChildPwd($username, $pwd, $token)
    {
        $params = [
            'username' => $username,
            'pwd' => $pwd
        ];
        $apiUrl = 'http://api.ipweb.cc:8004/api/agent/childAccount/updatePwd';
        $response = Http::withHeaders([
            'Token' => $token
        ])->post($apiUrl, $params);

        $result = $response->json();
        OperationLog::create([
            'api' => $apiUrl,
            'params' => json_encode($params, JSON_UNESCAPED_UNICODE),
            'result' => json_encode($result, JSON_UNESCAPED_UNICODE)
        ]);
        return $result;
    }

    /**
     * 修改子账号状态
     *
     * @return array
     * @author
     **/
    public function updateChildStatus($username, $status, $token)
    {
        $params = [
            'username' => $username,
            'status' => $status
        ];
        $apiUrl = 'http://api.ipweb.cc:8004/api/agent/childAccount/updateStatus';
        $response = Http::withHeaders([
            'Token' => $token
        ])->post($apiUrl, $params);

        $result = $response->json();
        OperationLog::create([
            'api' => $apiUrl,
            'params' => json_encode($params, JSON_UNESCAPED_UNICODE),
            'result' => json_encode($result, JSON_UNESCAPED_UNICODE)
        ]);
        return $result;
    }

    /**
     * 获取子账号列表
     *
     * @return array
     * @author
     **/
    public function getUserList($auth_token)
    {
        $apiUrl = 'http://api.ipweb.cc:8004/api/agent/childAccount/users';
        $response = Http::withHeaders([
            'Token' => $auth_token
        ])->post($apiUrl);

        $result = $response->json();
        return $result;
    }

    /**
     * 修改子账号流量限制
     *
     * @return array
     * @author
     * @doc https://user.ipweb.cc/api/v2/static-residential.html
     **/
    public function updateChildLimitFlow($username, $limitFlow, $token)
    {
        $params = [
            'username' => $username,
            'limitFlow' => $limitFlow
        ];
        $apiUrl = 'http://api.ipweb.cc:8004/api/agent/childAccount/updateLimitFlow';
        $response = Http::withHeaders([
            'Token' => $token
        ])->post($apiUrl, $params);
        if ($response->status() != 200) {

        }
        return $response->json();
    }

    /**
     * 获取静态代码地区
     *
     * @return array
     * @author
     **/
    public function getStaticRegions($country_code = '')
    {
        if (empty($country_code)) {
            $apiUrl = 'https://user.ipweb.cc/prod-api/v2/static-residential/listCountry';
            $response = Http::withHeaders([
                'Token' => $this->token
            ])->get($apiUrl);
            if ($response->status() != 200) {
                return [];
            }
            return $response->json();
        } else {
            $params = [
                'countryCode' => $country_code
            ];
            $apiUrl = 'https://user.ipweb.cc/prod-api/v2/static-residential/listCity';
            $response = Http::withHeaders([
                'Token' => $this->token
            ])->get($apiUrl, $params);
            if ($response->status() != 200) {
                return [];
            }
            return $response->json();
        }
    }

    /**
     * 创建静态代码订单
     *
     * @return array
     * @author
     **/
    public function createStaticOrder($country_code, $days, $count, $line_type, $business, $customer_id)
    {
        $apiUrl = 'https://user.ipweb.cc/prod-api/v2/static-residential/newIP';
        $params = [
            'country_code' => $country_code,
            'days' => (int)$days,
            'count' => (int)$count,
            'line_type' => (int)$line_type,
            'is_udp' => 0,
            'business' => $business,
            // 'customer_id' => $customer_id,
        ];
        $response = Http::withHeaders([
            'Token' => $this->token
        ])->post($apiUrl, $params);

        $result = $response->json();
        OperationLog::create([
            'api' => $apiUrl,
            'params' => json_encode($params, JSON_UNESCAPED_UNICODE),
            'result' => json_encode($result, JSON_UNESCAPED_UNICODE)
        ]);

        return $result;
    }

    /**
     * 静态代理续费
     *
     * @return array
     * @author
     **/
    public function staticOrderRenew($ip, $days, $customer_id = '')
    {
        $apiUrl = 'https://user.ipweb.cc/prod-api/v2/static-residential/renewIP';
        $params = [
            'ip' => $ip,
            'days' => $days,
            // 'customer_id' => $customer_id,
        ];
        $response = Http::withHeaders([
            'Token' => $this->token
        ])->post($apiUrl, $params);

        $result = $response->json();
        OperationLog::create([
            'api' => $apiUrl,
            'params' => json_encode($params, JSON_UNESCAPED_UNICODE),
            'result' => json_encode($result, JSON_UNESCAPED_UNICODE)
        ]);

        return $result;
    }

    /**
     * 业务用途
     *
     * @return array
     * @author
     **/
    public function business()
    {
        $apiUrl = 'https://user.ipweb.cc/prod-api/v2/static-residential/listBusiness';
        $response = Http::withHeaders([
            'Token' => $this->token
        ])->get($apiUrl);

        return $response->json();
    }

    /**
     * 业务用途
     *
     * @return int
     * @author
     **/
    public function stock($country_code, $business, $line_type)
    {
        $apiUrl = 'https://user.ipweb.cc/prod-api/v2/static-residential/countIdleIP';
        $params = [
            'country_code' => $country_code,
            'business' => $business,
            'line_type' => $line_type
        ];
        $response = Http::withHeaders([
            'Token' => $this->token
        ])->timeout(3)->post($apiUrl, $params);

        $result = $response->json();
        if ($result['code'] == 200 && $result['data'] > 0) {
            return $result['data'];
        }
        return 0;
    }
} // END class
